package com.superman.supermarket.client.composite.collection;

import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.supermarket.client.composite.IUIModule;
import com.superman.supermarket.client.constant.GoodsConstant;
import com.superman.supermarket.client.event.CollectionPageEvent;
import com.superman.supermarket.client.event.CollectionPageEvent.CollectionPageEventType;
import com.superman.supermarket.client.utils.ActivityJumpUrl;

/**
 * 收藏夹模块
 * 
 * @author superman
 * @version 2018年8月7日上午10:58:51
 */
public class CollectionModule extends Composite implements IUIModule {

	interface CollectionModuleUiBinder extends UiBinder<Widget, CollectionModule> {
	}

	private static CollectionModuleUiBinder uiBinder = GWT.create(CollectionModuleUiBinder.class);

	@UiField
	FlowPanel mainpanel;
	@UiField
	Image skuimg;
	@UiField
	HTML skunamehtml, salepricehtml, invalidhtml;

	private String spuId, prop;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月7日 上午10:58:49
	 */
	public CollectionModule() {
		initWidget(uiBinder.createAndBindUi(this));

		mainpanel.addDomHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				event.stopPropagation();
				ActivityJumpUrl.pushToSkuDetailPage(spuId, prop);
			}
		}, ClickEvent.getType());
	}

	@UiHandler("deletehtml")
	void delete(ClickEvent event) {
		event.stopPropagation();
		CollectionPageEvent collectionPageEvent = new CollectionPageEvent(CollectionPageEventType.DELETE, spuId);
		XGinjector.INSTANCE.getEventbusController().fire(collectionPageEvent);
	}

	@Override
	public Map<String, Object> getData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setData(Map<String, Object> map) {
		this.spuId = String.valueOf(map.get("SPU_ID"));
		this.prop = String.valueOf(map.get("PROPS"));
		skuimg.setUrl(String.valueOf(map.get("PIC")));
		skunamehtml.setText(String.valueOf(map.get("NAME")));
		salepricehtml.setHTML("¥&nbsp;" + String.valueOf(map.get("SALE_PRICE")));

		boolean valid = Double.parseDouble(String.valueOf(map.get("STATE"))) == GoodsConstant.GOODS_STATE_ONSALE;

		if (!valid) {
			salepricehtml.removeStyleName("base-color");
			skunamehtml.getElement().getStyle().setProperty("color", "#999");
			salepricehtml.getElement().getStyle().setProperty("color", "#999");
			invalidhtml.setVisible(true);
		}
	}

}
